package com.hk.service.impl.view;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.hk.commons.HqlFilter;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.Organization;
import com.hk.commons.entity.view.UserView;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.IOrganization;
import com.hk.service.sys.ISysUserRoles;
import com.hk.service.view.IUserView;

@Service
public class UserViewImpl extends BaseServiceImpl<UserView> implements
		IUserView {
	
	private Logger logger = LoggerFactory.getLogger(UserViewImpl.class);
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private ISysUserRoles roleService;
	
	@Autowired
	private IOrganization orgService;
	

	 
	@Override
	public List<UserView> getAllSubUser(long userId, String userRealName, String userOrgName,
			int curPage, int pagSize) {
		logger.info("userId ={}, userRealName={}, userOrgName ={},  curPage ={}, pagSize={}",userId, userRealName, userOrgName, curPage, pagSize);
		
		HqlFilter hqlFilter = getFilter(userId, userRealName, userOrgName);
		
		if(hqlFilter == null){
			return null;
		}
		List<UserView> viewList = findByFilter(hqlFilter, curPage, pagSize);
		
		return viewList;
	}


	@Override
	public UserView findById(long userId) {
		
		return getById(userId);
	}


	@Override
	public Long getTotalCount(long userId, String userRealName,
			String userOrgName) {
		Long size = Long.valueOf("0");
		HqlFilter hqlFilter = getFilter(userId, userRealName, userOrgName);
		
		if(hqlFilter != null){
			size = countByFilter(hqlFilter);
		}
		
		return size;
	}
	
	public HqlFilter getFilter(long userId, String userRealName,
			String userOrgName){
		
		UserView user = getById(userId);
		
		if(user == null){
			return null;
		}
		
		HqlFilter hqlFilter = new HqlFilter();
		
		hqlFilter.addSql(" where 1=1 ");
		
		
		// 如果不是运营部的人员，则添加筛选条件
		Boolean isOpear = roleService.isOperationalManager(userId);
		logger.info("userId ={}, isOpear = {}" , userId, isOpear);
		
		
		if (!isOpear) {
			// 增加组织架构的筛选
			Organization org = orgService.findById(user.getUserOrgId());
			// 如果用户的组织架构不为空，且为部门负责人
			if (org != null && org.getIsHeader() == 1) {
				String code = orgService.getOrgLeaderCodeByOrgId(org
						.getId());
				hqlFilter
						.addSql(" and ( userOrgCode like '" + code + "%') ");
			} else {
				hqlFilter.addSql(" and userId  = " + userId + " )");
			}	 
		}
		
		if(StringHelper.isNotEmpty(userRealName)){
		
			hqlFilter.addSql(" and (userRealName like '%"  + userRealName + "%'or userPhone like '%"  + userRealName + "%') ");
		}

		if(StringHelper.isNotEmpty(userOrgName)){
			
			hqlFilter.addSql(" and userOrgName like '%"  + userOrgName + "%'");
		}
		
		return hqlFilter;
		
	}
}
